#!/bin/sh

# set option -x, for debug shell
# trace shell execution, show all of command
set -x

# define a variable, value is execution result of command `pwd`
SOURCE_DIR=`pwd`

# :- , set variable value if it is not initialized or null
# :+ , set variable value if it is null or not
BUILD_DIR=${BUILD_DIR:-${SOURCE_DIR}/build}
BUILD_TYPE=${BUILD_TYPE:-release}
INSTALL_DIR=${INSTALL_DIR:-${SOURCE_DIR}/${BUILD_TYPE}-install-cpp11}
CXX=${CXX:-g++}

# for clang compiler
# ln, make links
# -s make symbolic links instead of hard links
# -f remove existing destination files
# force create a symbolic link to target file
ln -sf $BUILD_DIR/$BUILD_TYPE-cpp11/compile_commands.json

mkdir -p $BUILD_DIR/$BUILD_TYPE-cpp11 \
  && cd $BUILD_DIR/$BUILD_TYPE-cpp11

cmake \
  -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
  -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
  -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
  -S $SOURCE_DIR -B $BUILD_DIR/$BUILD_TYPE-cpp11 \
  && make &*

# Use the following command to run all the unit tests
# at the dir $BUILD_DIR/$BUILD_TYPE :
# CTEST_OUTPUT_ON_FAILURE=TRUE make test

# cd $SOURCE_DIR && doxygen
